package com.zhss.eshop.wms.stock;

import com.zhss.eshop.common.util.ObjectUtils;
import com.zhss.eshop.wms.domain.dto.WmsGoodsAllocationStockDetailDTO;
import com.zhss.eshop.wms.domain.dto.WmsPurchaseInputOrderDTO;
import com.zhss.eshop.wms.domain.dto.WmsPurchaseInputOrderItemDTO;
import com.zhss.eshop.wms.domain.dto.WmsPurchaseInputOrderPutOnItemDTO;
import com.zhss.eshop.wms.domain.model.WmsGoodsAllocationStock;
import com.zhss.eshop.wms.domain.model.WmsGoodsAllocationStockDetail;
import com.zhss.eshop.wms.domain.model.WmsGoodsStock;
import com.zhss.eshop.wms.mapper.WmsGoodsAllocationStockDetailMapper;
import com.zhss.eshop.wms.mapper.WmsGoodsAllocationStockMapper;
import com.zhss.eshop.wms.mapper.WmsGoodsStockMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * 采购入库库存更新组件
 * @author suyuan
 *
 */
@Component
@Scope("prototype")
public class PurchaseInputWmsStockUpdater extends AbstractWmsStockUpdater {

	/**
	 * 采购入库单
	 */
	private WmsPurchaseInputOrderDTO purchaseInputOrder;

	/**
	 * 商品库存管理的DAO组件
	 */
	@Autowired
	private WmsGoodsStockMapper goodsStockDAO;
	/**
	 * 货位库存管理的DAO组件
	 */
	@Autowired
	private WmsGoodsAllocationStockMapper goodsAllocationStockDAO;
	/**
	 * 货位库存明细管理的DAO组件
	 */
	@Autowired
	private WmsGoodsAllocationStockDetailMapper stockDetailDAO;

	/**
	 * 更新商品库存
	 */
	@Override
	protected void updateGoodsStock() throws Exception {
		List<WmsPurchaseInputOrderItemDTO> purchaseInputOrderItems =
				purchaseInputOrder.getItems();
		for(WmsPurchaseInputOrderItemDTO purchaseInputOrderItem : purchaseInputOrderItems) {
			WmsGoodsStock goodsStock = goodsStockDAO.getBySkuId(purchaseInputOrderItem.getGoodsSkuId());
			if(goodsStock == null) {
				goodsStock = new WmsGoodsStock();
				goodsStock.setGoodsSkuId(purchaseInputOrderItem.getGoodsSkuId());
				goodsStock.setAvailableStockQuantity(0L);
				goodsStock.setLockedStockQuantity(0L);
				goodsStock.setOutputStockQuantity(0L);
				goodsStockDAO.insertSelective(goodsStock);
			}
			goodsStock.setAvailableStockQuantity(goodsStock.getAvailableStockQuantity()
					+ purchaseInputOrderItem.getArrivalCount());
			goodsStockDAO.updateByPrimaryKeySelective(goodsStock);
		}
	}

	/**
	 * 更新货位库存
	 */
	@Override
	protected void updateGoodsAllocationStock() throws Exception {
		List<WmsPurchaseInputOrderItemDTO> items = purchaseInputOrder.getItems();

		for(WmsPurchaseInputOrderItemDTO item : items) {
			List<WmsPurchaseInputOrderPutOnItemDTO> putOnItems = item.getPutOnItemDTOs();

			for(WmsPurchaseInputOrderPutOnItemDTO putOnItem : putOnItems) {
				WmsGoodsAllocationStock goodsAllocationStock = goodsAllocationStockDAO
						.getBySkuId(putOnItem.getGoodsAllocationId(), putOnItem.getGoodsSkuId());
				if(goodsAllocationStock == null) {
					goodsAllocationStock = new WmsGoodsAllocationStock();
					goodsAllocationStock.setGoodsAllocationId(putOnItem.getGoodsAllocationId());
					goodsAllocationStock.setGoodsSkuId(putOnItem.getGoodsSkuId());
					goodsAllocationStock.setAvailableStockQuantity(0L);
					goodsAllocationStock.setLockedStockQuantity(0L);
					goodsAllocationStock.setOutputStockQuantity(0L);
					goodsAllocationStockDAO.insertSelective(goodsAllocationStock);
				}
				goodsAllocationStock.setAvailableStockQuantity(goodsAllocationStock.getAvailableStockQuantity()
						+ putOnItem.getPutOnShelvesCount());
				goodsAllocationStockDAO.updateByPrimaryKeySelective(goodsAllocationStock);
			}
		}
	}

	/**
	 * 更新货位库存明细
	 */
	@Override
	protected void updateGoodsAllocationStockDetail() throws Exception {
		List<WmsPurchaseInputOrderItemDTO> items = purchaseInputOrder.getItems();

		for(WmsPurchaseInputOrderItemDTO item : items) {
			List<WmsPurchaseInputOrderPutOnItemDTO> putOnItems = item.getPutOnItemDTOs();
			List<WmsGoodsAllocationStockDetail> stockDetails =
					new ArrayList<WmsGoodsAllocationStockDetail>();

			for(WmsPurchaseInputOrderPutOnItemDTO putOnItem : putOnItems) {
				WmsGoodsAllocationStockDetail stockDetail = new WmsGoodsAllocationStockDetail();
				stockDetail.setGoodsAllocationId(putOnItem.getGoodsAllocationId());
				stockDetail.setGoodsSkuId(putOnItem.getGoodsSkuId());
				stockDetail.setPutOnQuantity(putOnItem.getPutOnShelvesCount());
				stockDetail.setPutOnTime(putOnItem.getGmtCreate());
				stockDetail.setCurrentStockQuantity(stockDetail.getPutOnQuantity());
				stockDetail.setLockedStockQuantity(0L);

				stockDetailDAO.insertSelective(stockDetail);

				stockDetails.add(stockDetail);
			}

			item.setStockDetails(ObjectUtils.convertList(stockDetails,
					WmsGoodsAllocationStockDetailDTO.class));
		}
 	}

	/**
	 * 设置需要的参数
	 */
	@Override
	public void setParameter(Object parameter) {
		this.purchaseInputOrder = (WmsPurchaseInputOrderDTO) parameter;
	}

}
